﻿using System;
using System.IO;

namespace Ant.Interface.Environment.Configuration
{
    internal class ShellSettingsSerializer
    {
        public const char Separator = ':';
        public const string EmptyValue = "null";
        public const char ThemesSeparator = ';';

        public static ShellSettings ParseSettings(string text)
        {
            var shellSettings = new ShellSettings();
            if (string.IsNullOrEmpty(text))
                return shellSettings;

            var settings = new StringReader(text);
            string setting;
            while ((setting = settings.ReadLine()) != null)
            {
                if (string.IsNullOrWhiteSpace(setting)) continue;
                var separatorIndex = setting.IndexOf(Separator);
                if (separatorIndex == -1)
                {
                    continue;
                }
                var key = setting.Substring(0, separatorIndex).Trim();
                var value = setting.Substring(separatorIndex + 1).Trim();

                if (value == EmptyValue) continue;
                switch (key)
                {
                    case "Name":
                        shellSettings.Name = value;
                        break;

                    case "DataProvider":
                        shellSettings.DataProvider = value;
                        break;

                    case "State":
                        shellSettings.State = new TenantState(value);
                        break;

                    case "DataConnectionString":
                        shellSettings.DataConnectionString = value;
                        break;

                    case "DataPrefix":
                        shellSettings.DataTablePrefix = value;
                        break;

                    case "RequestUrlHost":
                        shellSettings.RequestUrlHost = value;
                        break;

                    case "RequestUrlPrefix":
                        shellSettings.RequestUrlPrefix = value;
                        break;

                    case "Themes":
                        shellSettings.Themes = value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        break;
                }
            }

            return shellSettings;
        }

        public static string ComposeSettings(ShellSettings settings)
        {
            if (settings == null)
                return string.Empty;

            return string.Format("Name: {0}\r\nDataProvider: {1}\r\nDataConnectionString: {2}\r\nDataPrefix: {3}\r\nRequestUrlHost: {4}\r\nRequestUrlPrefix: {5}\r\nState: {6}\r\nThemes: {7}\r\n",
                                 settings.Name,
                                 settings.DataProvider,
                                 settings.DataConnectionString ?? EmptyValue,
                                 settings.DataTablePrefix ?? EmptyValue,
                                 settings.RequestUrlHost ?? EmptyValue,
                                 settings.RequestUrlPrefix ?? EmptyValue,
                                 settings.State != null ? settings.State.ToString() : String.Empty,
                                 string.Join(";", settings.Themes ?? new string[0])
                );
        }
    }
}